package interfaces;

import plantObjects.*;
import java.util.*;

/**
 * Logical Database for Start and Endstorage
 * @author iii
 */
public class storageDB {

    static String reserved = "reserved";
    static TreeMap<Position, String> startStorage = new TreeMap<Position, String>();
    static TreeMap<Position, String> endStorage = new TreeMap<Position, String>(); //(max storage == 100)

    /**
     * Constructor for initializing State at time=0
     * @param TreeMap<Position, String> startStorage
     * @param TreeMap<Position, String> endStorage
     */
    public storageDB(TreeMap<Position, String> startStorage, TreeMap<Position, String> endStorage)
    {
        storageDB.startStorage = startStorage;
        storageDB.endStorage = endStorage;
    }

    /**
     * returns a List of Positions with Length = numOfBars for given barTypes
     * from the StartStorage to get to conveyor
     * logically removes (i.e. reserves) the bars; always successful
     * @param barType
     * @param numOfBars
     * @return ArrayList<Position>
     */
    public static ArrayList<Position> getLongBars(String barType, int numOfBars) {
        
        ArrayList<Position> bars = new ArrayList<Position>();
        String type;

        // search through all Positions p
        for (Position p : startStorage.keySet()) {
            //if String at Position p == barType create List with numOfBars times p
            type = startStorage.get(p);
            if (0 == type.compareTo(barType)) {
                for (int i = 1; i <= numOfBars; i++) {
                    bars.add(p);
                }
                break;
            }
        }

        return bars;
    }

    /**
     * returns a List of Positions with Length <= numOfBars for given barTypes
     * from the EndStorage to get to conveyor
     * changes String to "reserved"
     * @param barType
     * @param numOfBars
     * @return ArrayList<Position> or null if List is empty
     */
    public static ArrayList<Position> getShortBars(String barType, int numOfBars) {
        
        ArrayList<Position> bars = new ArrayList<Position>();
        String type;

        // search through all Positions p
        for (Position p : endStorage.keySet()) {
            //if String at Position p == barType add Position p to List
            type = endStorage.get(p);
            if (0 == type.compareTo(barType)) {
                bars.add(p);
                // reserve this position so it can't be found in future searches
                endStorage.remove(p);
                endStorage.put(p,reserved);
                //stop loop if all Bars found
                if (bars.size() >= numOfBars) {
                    break;
                }
            }
        }

        if (bars.isEmpty()) {
            return null;
        }
        else return bars;
    }

    /**
     *  returns a List of Positions with Length = numOfBars for given barTypes
     * to be put in the EndStorage
     * changes String to "reserved"
     * @param barType
     * @param numOfBars
     * @return ArrayList<Position> or null if EndStorage.size() >= 100
     */
    public static ArrayList<Position> addShortBars(String barType, int numOfBars) {
        
        ArrayList<Position> bars = new ArrayList<Position>();

        //only do, if there is enough space for all Bars to be added
        if (availableSpace() >= numOfBars) {
            //search through all possible positions
            for (int i = 1; i <= 10; i++) {
                for (int j = 1; j <= 10; i++) {
                    //if Position is free, add Position p to List
                    if (!endStorage.containsKey(new Position(i, j))) {
                        bars.add(new Position(i, j));
                        // reserve this position so it can't be found in future searches
                        endStorage.put(new Position(i, j), reserved);
                    }

                }
            }
            return bars;
        }
        else return null;
    }

    /**
     * Removes a Reserved Entry in ArrayList endStorage
     * prints error, if Position not reserved
     * @param Position p
     */
    public static void removeReservedGet(Position p)
    {
        String type = endStorage.get(p);
        
        if (0 == type.compareTo(reserved)) endStorage.remove(p);
        else System.err.println("This position was not reserved, nothing was deleted");
    }

    /**
     * Adds a new Entry barType to a Reserved Position p in ArrayLisst endStorage
     * prints error, if Position not reserved
     * @param Position p
     * @param String barType
     */
    public static void removeReservedAdd(Position p, String barType)
    {
        String type = endStorage.get(p);

        if (0 == type.compareTo(reserved))
        {
            endStorage.remove(p);
            endStorage.put(p, barType);
        }
        else System.err.println("This position was not reserved, nothing was added");
    }

    /**
     * Returns availableSpace left in endStorage
     * @return 100 - endStorage.size()
     */
    private static int availableSpace() {
        return 100 - endStorage.size();
    }
}
